package org.deschutter.scans;

import org.deschutter.constants.Config;
import org.deschutter.constants.Formulae;
import org.deschutter.exceptions.AddPlanetException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: Berten De Schutter
 * Date: 01-Jul-2009
 * Time: 11:27:21
 */
public class Planet extends Scan {
    private BigDecimal agents, securityGuards, mroids, croids, eroids, mresources, cresources, eresources, prodres, lightprod, medprod, heavyprod;
    private String light, medium, heavy;

    public Planet(String ID, BigDecimal tick, String x, String y, String z, String scanpage, BigDecimal userID, Session session) {
        super(ID, Config.PLANET_SCAN, tick, x, y, z, scanpage, userID, session);
    }

    @Override
    protected String parseScan() {


        Pattern compiledRegex;
        Matcher regexMatcher;

        // Get Roids + Resources
        compiledRegex = Pattern.compile("<tr><td[^>]*>Metal</td><td[^>]*>(\\d+)</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Crystal</td><td[^>]*>(\\d+)</td><td[^>]*>(\\d+)</td></tr>\\s*" +
                "<tr><td[^>]*>Eonium</td><td[^>]*>(\\d+)</td><td[^>]*>(\\d+)</td></tr>\\s*");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();
        mroids = new BigDecimal(regexMatcher.group(1));
        mresources = new BigDecimal(regexMatcher.group(2));
        croids = new BigDecimal(regexMatcher.group(3));
        cresources = new BigDecimal(regexMatcher.group(4));
        eroids = new BigDecimal(regexMatcher.group(5));
        eresources = new BigDecimal(regexMatcher.group(6));


        // Get Agents & SecurityGuards
        compiledRegex = Pattern.compile("<tr><th[^>]*>Agents</th><th[^>]*>Security\\s+Guards</th></tr>\\s*<tr><td[^>]*>([^<]+)</td><td[^>]*>([^<]+)</td></tr>\\s*", 0);
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();

        agents = new BigDecimal(regexMatcher.group(1));
        securityGuards = new BigDecimal(regexMatcher.group(2));

        // Get Production stats

        compiledRegex = Pattern.compile("<tr><th[^>]*>Light</th><th[^>]*>Medium</th><th[^>]*>Heavy</th></tr>\\s*" +
                "<tr><td[^>]*>([^<]+)</td><td[^>]*>([^<]+)</td><td[^>]*>([^<]+)</td></tr>");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();

        light = regexMatcher.group(1);
        medium = regexMatcher.group(2);
        heavy = regexMatcher.group(3);

        // Get Resources In Production
        compiledRegex = Pattern.compile("<span[^>]*>(\\d+)</span>");
        regexMatcher = compiledRegex.matcher(getScanpage().replace(",", ""));
        regexMatcher.find();

        prodres = new BigDecimal(regexMatcher.group(1));

        // Get max amount of res in production for each Factory

        lightprod = Formulae.getResourcesInProduction(light, prodres);
        medprod = Formulae.getResourcesInProduction(medium, prodres);
        heavyprod = Formulae.getResourcesInProduction(heavy, prodres);
        try {
            addPlanetDetails();
        } catch (AddPlanetException e) {
            return e.getError();
        }
        return "Planet Scan on " + getX() + ":" + getY() + ":" + getZ() + " parsed successfully";
    }

    public void addPlanetDetails() throws AddPlanetException {
        org.deschutter.hibernate.Planet hbplanet = new org.deschutter.hibernate.Planet();

        Transaction tx = null;
        try {
            hbplanet.setScansByScanId(getHbscan());

            hbplanet.setMroids(mroids.intValue());
            hbplanet.setCroids(croids.intValue());
            hbplanet.setEroids(eroids.intValue());

            hbplanet.setCresources(cresources.longValue());
            hbplanet.setMresources(mresources.longValue());
            hbplanet.setEresources(eresources.longValue());

            hbplanet.setGuards(securityGuards.intValue());
            hbplanet.setSecurityAgents(agents.intValue());

            hbplanet.setHeavyFactory(heavy);
            hbplanet.setLightFactory(light);
            hbplanet.setMediumFactory(medium);

            hbplanet.setLightProduction(lightprod.intValue());
            hbplanet.setMediumProduction(medprod.intValue());
            hbplanet.setHeavyProduction(heavyprod.intValue());

            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(hbplanet);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) tx.rollback();
            throw new AddPlanetException(e.getMessage());
        }


        //Queries.addPlanetDetails(getID(),getX(),getY(),getZ(),getTick(),agents, securityGuards, mroids, croids, eroids, mresources, cresources, eresources, prodres, lightprod, medprod, heavyprod,light,medium,heavy);
    }

    public BigDecimal getAgents() {
        return agents;
    }

    public void setAgents(BigDecimal agents) {
        this.agents = agents;
    }

    public BigDecimal getSecurityGuards() {
        return securityGuards;
    }

    public void setSecurityGuards(BigDecimal securityGuards) {
        this.securityGuards = securityGuards;
    }

    public BigDecimal getMroids() {
        return mroids;
    }

    public void setMroids(BigDecimal mroids) {
        this.mroids = mroids;
    }

    public BigDecimal getCroids() {
        return croids;
    }

    public void setCroids(BigDecimal croids) {
        this.croids = croids;
    }

    public BigDecimal getEroids() {
        return eroids;
    }

    public void setEroids(BigDecimal eroids) {
        this.eroids = eroids;
    }

    public BigDecimal getMresources() {
        return mresources;
    }

    public void setMresources(BigDecimal mresources) {
        this.mresources = mresources;
    }

    public BigDecimal getCresources() {
        return cresources;
    }

    public void setCresources(BigDecimal cresources) {
        this.cresources = cresources;
    }

    public BigDecimal getEresources() {
        return eresources;
    }

    public void setEresources(BigDecimal eresources) {
        this.eresources = eresources;
    }

    public BigDecimal getProdres() {
        return prodres;
    }

    public void setProdres(BigDecimal prodres) {
        this.prodres = prodres;
    }

    public BigDecimal getLightprod() {
        return lightprod;
    }

    public void setLightprod(BigDecimal lightprod) {
        this.lightprod = lightprod;
    }

    public BigDecimal getMedprod() {
        return medprod;
    }

    public void setMedprod(BigDecimal medprod) {
        this.medprod = medprod;
    }

    public BigDecimal getHeavyprod() {
        return heavyprod;
    }

    public void setHeavyprod(BigDecimal heavyprod) {
        this.heavyprod = heavyprod;
    }

    public String getLight() {
        return light;
    }

    public void setLight(String light) {
        this.light = light;
    }

    public String getMedium() {
        return medium;
    }

    public void setMedium(String medium) {
        this.medium = medium;
    }

    public String getHeavy() {
        return heavy;
    }

    public void setHeavy(String heavy) {
        this.heavy = heavy;
    }

    public BigDecimal getTypeID() {
        return SCAN_TYPE_PLANET;
    }
}
